package 传智播客_黑马.java高级.多线程;

public class 线程安全问题 {
    //起因:多线程访问共享数据


    //实现买票案例
    class Runimpl implements Runnable{
        //定义共享票源
        private int ticket = 100;

        //设置线程任务
        @Override
        public void run() {
            //提高概率
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            while(true){
                if(ticket>0){
                    System.out.println(Thread.currentThread().getName()+"-->正在卖"+(ticket--)+"张票");
                }
            }
        }
    }


    public void test(){
        Runimpl runimpl = new Runimpl();
        Thread thread1 = new Thread(runimpl);
        Thread thread2 = new Thread(runimpl);
        Thread thread3 = new Thread(runimpl);
        thread1.start();
        thread2.start();
        thread3.start();
    }
    public static void main(String[] args) {
        new 线程安全问题().test();
    }
}
